package com.wcgksoft.controller.mainten;


import com.wcgksoft.common.util.FileHandleUtil;
import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.ConstructionTeam.ConstructionTeam;
import com.wcgksoft.entity.GreedLand.GreedLand;
import com.wcgksoft.entity.mainten.Mainten;
import com.wcgksoft.entity.mainten.MaintenPhoto;
import com.wcgksoft.entity.mainten.MaintenType;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.ConstructionTeam.ConstructionTeamService;
import com.wcgksoft.service.EnterpriseProjectService;
import com.wcgksoft.service.GreedLand.GreedlandService;
import com.wcgksoft.service.mainten.MaintenPhotoService;
import com.wcgksoft.service.mainten.MaintenService;
import com.wcgksoft.service.mainten.MaintenTypeService;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 养护
 *
 * @author 
 */
@Controller
@RequestMapping("mainten")
public class MaintenController {



    @Autowired
    private MaintenService maintenService;
    @Autowired
    private MaintenTypeService maintenTypeService;
    @Autowired
    private GreedlandService greedlandService;

    @Autowired
    private ConstructionTeamService constructionTeamService;
    @Autowired
    private EnterpriseProjectService enterpriseProjectService;
    @Autowired
    private MaintenPhotoService maintenPhotoService;

    /**
     * 列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public Layui list(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit,
            @RequestParam(value = "type") Integer type,
            @RequestParam(value = "keyWord", required = false,defaultValue ="-1") Integer keyWord) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Mainten.class);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        criteria.add(Restrictions.eq("departid", sysUser.getProId()+""));
        if(sysUser.getTeam_id()!=null){
            criteria.add(Restrictions.eq("maintenanceTroops", sysUser.getTeam_id()+""));
        }
        criteria.add(Restrictions.eq("areaid", enterpriseProject.getId()+""));
        if(!keyWord.equals(-1)) {
            criteria.createAlias("maintenType", "maintenType");
            criteria.add(Restrictions.eq("maintenType.id", keyWord));
        }
        criteria.add(Restrictions.eq("type", type));
        List<Mainten> list = maintenService.findByConditions(criteria, page, limit);
        if(list.size()>0){
            for (int i = 0;i<list.size();i++){
                //队伍
                String duid = list.get(i).getMaintenanceTroops();
                ConstructionTeam constructionTeam = constructionTeamService.findById(Integer.valueOf(duid));
                list.get(i).setConstructionTeam(constructionTeam);
                //片区
                String areaid = list.get(i).getAreaid();
                EnterpriseProject area = enterpriseProjectService.findById(Integer.valueOf(areaid));
                list.get(i).setArea(area);

                DetachedCriteria criteria2 = DetachedCriteria.forClass(MaintenPhoto.class);
                criteria2.add(Restrictions.eq("maintenId",list.get(i).getId()));
                List<MaintenPhoto> byConditions = maintenPhotoService.findByConditions(criteria2);
                list.get(i).setMaintenPhotoList(byConditions);
            }



        }
        return Layui.data(0, "查询成功", maintenService.getTotalCount(), list);
    }

    /*
     * 修改
     *
     * */
    @ResponseBody
    @RequestMapping(value = "update")
    public Layui update(HttpServletRequest request, @RequestParam(value = "data", required = false) String data,  @RequestParam(value = "file", required = false) MultipartFile[] file) throws Exception {
        JSONObject param = JSONObject.fromObject(data);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        if (param == null) {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            Mainten mainten = (Mainten) JSONObject.toBean(JSONObject.fromObject(data), Mainten.class);
            String ids = String.valueOf(param.get("id"));
            if (ids.equals("")) {
                Date currentTime = new Date();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dateString = formatter.format(currentTime);
                mainten.setMaintenanceDate(dateString);
                mainten.setMaintenanceTroops(sysUser.getTeam_id()+"");
                mainten.setUuisid(sysUser.getId()+"");
            }

            String greedLand = String.valueOf(param.get("greedLand_id"));
            Integer greedLand_id=Integer.valueOf(greedLand);
            GreedLand byId = greedlandService.findById(greedLand_id);
            mainten.setGreedLand(byId);
            String maintenType = String.valueOf(param.get("maintenType_id"));
            Integer maintenType_id=Integer.valueOf(maintenType);
            MaintenType byId1 = maintenTypeService.findById(maintenType_id);
            mainten.setMaintenType(byId1);


            EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
            mainten.setDepartid(sysUser.getProId()+"");
            mainten.setAreaid(enterpriseProject.getId()+"");
            mainten.setEnterpriseid(sysUser.getEnterprise().getId());
            maintenService.saveOrUpdate(mainten);
            for (MultipartFile mf : file) {
                MaintenPhoto maintenPhoto = new MaintenPhoto();
                if (!mf.isEmpty()) {
                    String filename = FileHandleUtil.Upset(mf);
                    maintenPhoto.setPhoto("upload/picture/" + FileHandleUtil.upload(mf.getInputStream(), "upload/picture/", filename, request));
                    maintenPhoto.setMaintenId(mainten.getId());
                    maintenPhoto.setTime(new Date());
                    maintenPhotoService.saveOrUpdate(maintenPhoto);
                }
            }
            return Layui.data(0, "修改成功", 1, Layui.addList(mainten));
        }
    }

    /*
       查询养护内容名称
       */
    @ResponseBody
    @RequestMapping(value = "getMaintenType")
    public List<MaintenType> getGreedLandName(HttpServletRequest request,
                                              @RequestParam(value = "type", required = false) Integer type) {
        DetachedCriteria criteria = DetachedCriteria.forClass(MaintenType.class);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        criteria.add(Restrictions.eq("enterpriseid", sysUser.getEnterprise().getId()));
        criteria.add(Restrictions.eq("state", 0));
        criteria.add(Restrictions.eq("type", type));
        List<MaintenType> list = maintenTypeService.findByConditions(criteria);
        return list;
    }
    /*
     * 删除
     *
     * */
    @ResponseBody
    @RequestMapping(value = "delete")
    public Layui delete(@RequestParam(value = "id") Integer id) {
        Mainten byId = maintenService.findById(id);
        maintenService.delete(byId);
        return Layui.data(1, "删除成功", 1, null);
    }
    /*
       查询施工队对应绿地
       */
    @ResponseBody
    @RequestMapping(value = "getGreedLandNamex")
    public List<GreedLand> getGreedLandNamex() {
        DetachedCriteria criteria = DetachedCriteria.forClass(GreedLand.class);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        List<GreedLand> list = new ArrayList<GreedLand>();
        String greenlandid= sysUser.getGreenlandid();
        String[] strArr = greenlandid.split(",");
        for (int i = 0; i < strArr.length; i++){
           GreedLand greedLand= greedlandService.findById(Integer.valueOf(strArr[i]));
           if(greedLand.getState().equals(0)){
               list.add(greedLand);
           }

        }


        return list;
    }


    /**
     * 管理端首页列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "sylist")
    public Layui sylist(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit
            ) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Mainten.class);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        criteria.add(Restrictions.eq("departid", sysUser.getProId()+""));
        criteria.add(Restrictions.eq("areaid", enterpriseProject.getId()+""));
        List<Mainten> list = maintenService.findByConditions(criteria, page, limit);
        if(list.size()>0){
            for (int i = 0;i<list.size();i++){
                //队伍
                String duid = list.get(i).getMaintenanceTroops();
                ConstructionTeam constructionTeam = constructionTeamService.findById(Integer.valueOf(duid));
                list.get(i).setConstructionTeam(constructionTeam);
                //片区
                String areaid = list.get(i).getAreaid();
                EnterpriseProject area = enterpriseProjectService.findById(Integer.valueOf(areaid));
                list.get(i).setArea(area);
                DetachedCriteria criteria2 = DetachedCriteria.forClass(MaintenPhoto.class);
                criteria2.add(Restrictions.eq("maintenId",list.get(i).getId()));
                List<MaintenPhoto> byConditions = maintenPhotoService.findByConditions(criteria2);
                list.get(i).setMaintenPhotoList(byConditions);
            }
        }
        return Layui.data(0, "查询成功", maintenService.getTotalCount(), list);
    }

    /**
     * 施工端首页列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "sglist")
    public Layui sglist(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit
           ) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Mainten.class);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        criteria.add(Restrictions.eq("maintenanceTroops", sysUser.getTeam_id()+""));
        criteria.add(Restrictions.eq("enterpriseid", sysUser.getEnterprise().getId()));
        List<Mainten> list = maintenService.findByConditions(criteria, page, limit);
        if(list.size()>0){
            for (int i = 0;i<list.size();i++){
                //队伍
                String duid = list.get(i).getMaintenanceTroops();
                ConstructionTeam constructionTeam = constructionTeamService.findById(Integer.valueOf(duid));
                list.get(i).setConstructionTeam(constructionTeam);
                //片区
                String areaid = list.get(i).getAreaid();
                EnterpriseProject area = enterpriseProjectService.findById(Integer.valueOf(areaid));
                list.get(i).setArea(area);

                DetachedCriteria criteria2 = DetachedCriteria.forClass(MaintenPhoto.class);
                criteria2.add(Restrictions.eq("maintenId",list.get(i).getId()));
                List<MaintenPhoto> byConditions = maintenPhotoService.findByConditions(criteria2);
                list.get(i).setMaintenPhotoList(byConditions);
            }



        }
        return Layui.data(0, "查询成功", maintenService.getTotalCount(), list);
    }
}
